package main;

import framework.Game;
import framework.Person;
import impl.business.MatrixBiz;
import impl.game.Every2every;
import impl.persons.Altruist;
import impl.persons.Dir;
import javenue.csv.Csv;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;

import java.util.Date;

/**
 * Main class
 *
 * @author Denis DIR Rozhnev
 */
public class Main {

    static Logger LOG = Logger.getLogger(Main.class);
    private static final int TURNS_COUNT = 100;
    private static final boolean CSV_ENABLED = false;


    public static void main(String[] args) {

        BasicConfigurator.configure();

        Game game = new Every2every();
//        Game game = new GridGame(4, 4, GridGame.Q4);

        Person[] players = new Person[2];
        players[0] = new Dir();
        players[1] = new Altruist();

/*
        Person[] players = new Person[16];
        for (int i = 0; i < players.length; i++) {
            players[i] = new Stupid();

        }
*/

        //AASS
        //SSTS
        //EESS
        //SSSS
/*
        players[0] = new Altruist();
        players[1] = new Altruist();
        players[6] = new Trigger();
        players[8] = new Egoist();
        players[9] = new Egoist();
*/


        MatrixBiz biz = new MatrixBiz(10, 8, 3, 0);

        game.init(players, biz);

        long t = System.currentTimeMillis();
        game.turn(TURNS_COUNT);
        t = System.currentTimeMillis() - t;

        LOG.info("Time:" + t);
        LOG.info("=== Scores ===");
        Csv.Writer writer;
        if (CSV_ENABLED)
            writer = new Csv.Writer("result." + (new Date().getTime()) + ".csv");
        int[] finalScores = game.getScores();

        if (LOG.isInfoEnabled() || CSV_ENABLED)
            for (int i = 0; i < finalScores.length; i++) {
                int res = finalScores[i];
                String player = players[i].getClass().getSimpleName();
                if (CSV_ENABLED)
                    writer.value(player).value(Integer.toString(res)).newLine();
                LOG.info(player + ":\t" + res);
            }

        float profit = game.getProfit() / TURNS_COUNT;
        int s = biz.getMatrixSum();
        LOG.info("Average turn profit: " + profit + " Matrix sum: " + s);
        if (CSV_ENABLED)
            writer.value("Average turn profit: " + profit + " Matrix sum: " + s).close();
        if (s != 0)
            LOG.info("P=" + profit / s);
    }
}
